package com.survey.system.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.survey.common.core.domain.TreeSelect;
import com.survey.common.core.domain.entity.SysDept;
import com.survey.common.utils.StringUtils;
import com.survey.common.utils.spring.SpringUtils;
import com.survey.system.util.SurveyTreeSelect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.survey.system.mapper.SurRegionMapper;
import com.survey.system.domain.SurRegion;
import com.survey.system.service.ISurRegionService;

import static net.bytebuddy.matcher.ElementMatchers.hasChild;

/**
 * 地区Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-23
 */
@Service
public class SurRegionServiceImpl implements ISurRegionService {
    @Autowired
    private SurRegionMapper surRegionMapper;

    /**
     * 查询地区
     *
     * @param regionId 地区主键
     * @return 地区
     */
    @Override
    public SurRegion selectSurRegionByRegionId(Long regionId) {
        return surRegionMapper.selectSurRegionByRegionId(regionId);
    }

    /**
     * 查询地区列表
     *
     * @param surRegion 地区
     * @return 地区
     */
    @Override
    public List<SurRegion> selectSurRegionList(SurRegion surRegion) {
        return surRegionMapper.selectSurRegionList(surRegion);
    }

    /**
     * 新增地区
     *
     * @param surRegion 地区
     * @return 结果
     */
    @Override
    public int insertSurRegion(SurRegion surRegion) {
        return surRegionMapper.insertSurRegion(surRegion);
    }

    /**
     * 修改地区
     *
     * @param surRegion 地区
     * @return 结果
     */
    @Override
    public int updateSurRegion(SurRegion surRegion) {
        return surRegionMapper.updateSurRegion(surRegion);
    }

    /**
     * 批量删除地区
     *
     * @param regionIds 需要删除的地区主键
     * @return 结果
     */
    @Override
    public int deleteSurRegionByRegionIds(Long[] regionIds) {
        return surRegionMapper.deleteSurRegionByRegionIds(regionIds);
    }

    /**
     * 删除地区信息
     *
     * @param regionId 地区主键
     * @return 结果
     */
    @Override
    public int deleteSurRegionByRegionId(Long regionId) {
        return surRegionMapper.deleteSurRegionByRegionId(regionId);
    }

    @Override
    public List<SurveyTreeSelect> selectSurRegionTreeList(SurRegion surRegion) {
        List<SurRegion> surRegionList = selectSurRegionList(surRegion);
        return buildSurRegionTreeSelect(surRegionList);
    }

    private List<SurveyTreeSelect> buildSurRegionTreeSelect(List<SurRegion> surRegionList) {
        List<SurRegion> deptTrees = buildSurRegionTree(surRegionList);
        return deptTrees.stream().map(SurveyTreeSelect::new).collect(Collectors.toList());
    }

    private List<SurRegion> buildSurRegionTree(List<SurRegion> surRegionList) {
        List<SurRegion> returnList = new ArrayList<SurRegion>();
        List<Long> tempList = surRegionList.stream().map(SurRegion::getRegionId).collect(Collectors.toList());
        for (SurRegion region : surRegionList) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(region.getParentId())) {
                recursionFn(surRegionList, region);
                returnList.add(region);
            }
        }
        if (returnList.isEmpty()) {
            returnList = surRegionList;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SurRegion> list, SurRegion t) {
        // 得到子节点列表
        List<SurRegion> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SurRegion tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SurRegion> list, SurRegion t) {
        return getChildList(list, t).size() > 0;
    }

    /**
     * 得到子节点列表
     */
    private List<SurRegion> getChildList(List<SurRegion> list, SurRegion t) {
        List<SurRegion> tlist = new ArrayList<SurRegion>();
        Iterator<SurRegion> it = list.iterator();
        while (it.hasNext()) {
            SurRegion n = (SurRegion) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getRegionId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }
}
